package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Course;
import com.ruoyi.system.service.ICourseService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.ExcelUtil;

/**
 * 课程 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-12-11
 */
@Controller
@RequestMapping("/system/course")
public class CourseController extends BaseController
{
    private String prefix = "system/course";
	
	@Autowired
	private ICourseService courseService;
	
	@RequiresPermissions("system:course:view")
	@GetMapping()
	public String course()
	{
	    return prefix + "/course";
	}
	
	/**
	 * 查询课程列表
	 */
	@RequiresPermissions("system:course:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Course course)
	{
		startPage();
        List<Course> list = courseService.selectCourseList(course);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出课程列表
	 */
	@RequiresPermissions("system:course:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Course course)
    {
    	List<Course> list = courseService.selectCourseList(course);
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        return util.exportExcel(list, "course");
    }
	
	/**
	 * 新增课程
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存课程
	 */
	@RequiresPermissions("system:course:add")
	@Log(title = "课程", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Course course)
	{		
		return toAjax(courseService.insertCourse(course));
	}

	/**
	 * 修改课程
	 */
	@GetMapping("/edit/{scoreId}")
	public String edit(@PathVariable("scoreId") Integer scoreId, ModelMap mmap)
	{
		Course course = courseService.selectCourseById(scoreId);
		mmap.put("course", course);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存课程
	 */
	@RequiresPermissions("system:course:edit")
	@Log(title = "课程", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Course course)
	{		
		return toAjax(courseService.updateCourse(course));
	}
	
	/**
	 * 删除课程
	 */
	@RequiresPermissions("system:course:remove")
	@Log(title = "课程", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(courseService.deleteCourseByIds(ids));
	}
	
}
